package com.mhc.suzuki.util;

import com.mhc.suzuki.constant.CustomerTypeEnum;
import com.mhc.suzuki.vo.CreditExcelVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Excel util, create excel sheet, cell and style.
 *
 * @param <T> generic class.
 */
@Slf4j
public class ExcelUtil<T> {

    public XSSFCellStyle getCellStyle(XSSFWorkbook workbook, boolean isHeader) {
        XSSFCellStyle style = workbook.createCellStyle();
        style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        style.setBorderRight(HSSFCellStyle.BORDER_THIN);
        style.setBorderTop(HSSFCellStyle.BORDER_THIN);
        style.setLocked(true);
        if (isHeader) {
            XSSFFont font = workbook.createFont();
            font.setColor(HSSFColor.BLACK.index);
            font.setFontHeightInPoints((short) 12);
            font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
            style.setFont(font);
            style.setAlignment(XSSFCellStyle.ALIGN_CENTER);
        }
        return style;
    }


    public void generateHeader(XSSFWorkbook workbook, XSSFSheet sheet, String[] headerColumns) {
        Row row = sheet.createRow(1);
        row.setHeightInPoints(30);
        for (int i = 0; i < headerColumns.length; i++) {
            XSSFCellStyle style = getCellStyle(workbook, true);

            Cell cell = row.createCell(i);
            String[] column = headerColumns[i].split("_#_");
            sheet.setColumnWidth(i, Integer.valueOf(column[1]));
            cell.setCellValue(column[0]);
            XSSFFont font = workbook.createFont();
            font.setFontName("仿宋_GB2312");
            font.setFontHeightInPoints((short) 12);
            style.setFont(font);
            if (column.length > 2) {
                if ("YELLOW".equals(column[2])) {
                    style.setFillForegroundColor(HSSFColor.YELLOW.index);
                    style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
                } else if ("BLUE".equals(column[2])) {
                    style.setFillForegroundColor(HSSFColor.BLUE.index);
                    style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
                }
            }
            cell.setCellStyle(style);
        }
    }

    public void generateTitle(XSSFWorkbook workbook, XSSFSheet sheet, String title) {
        XSSFCellStyle style = getCellStyle(workbook, false);
        Row row = sheet.createRow(0);
        row.setHeightInPoints(30);
        CellRangeAddress cellRangeAddress = new CellRangeAddress(0, 0, 0, 13);
        //在sheet里增加合并单元格
        sheet.addMergedRegion(cellRangeAddress);
        //生成第一行
        Cell cell = row.createCell(0);
        cell.setCellValue(title);
        XSSFFont font = workbook.createFont();
        font.setFontName("黑体");
        font.setFontHeightInPoints((short) 20);
        font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
        style.setFont(font);
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        cell.setCellStyle(style);

    }


    @SuppressWarnings({"rawtypes", "unchecked"})
    public XSSFSheet creatAuditSheet(XSSFWorkbook workbook, String bankName,
                                     List<CreditExcelVO> dataset, String[] headerColumns, String[] fieldColumns) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        XSSFSheet sheet = workbook.createSheet();
        sheet.protectSheet("");
        generateTitle(workbook, sheet, "浙江元通汽车征信查询清单("+bankName+"）");
        generateHeader(workbook, sheet, headerColumns);//生成标题和头部
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
        int rowNum = 1;
        for (CreditExcelVO creditExcelVO : dataset) {
            rowNum++;
            XSSFCellStyle style = getCellStyle(workbook, false);

            Row row = sheet.createRow(rowNum);
            row.setHeightInPoints(25);
            for (int i = 0; i < fieldColumns.length; i++) {
                String fieldName = fieldColumns[i];
                String getMethodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
                try {
                    Class clazz = creditExcelVO.getClass();
                    Method getMethod;
                    getMethod = clazz.getMethod(getMethodName, new Class[]{});
                    Object value = getMethod.invoke(creditExcelVO, new Object[]{});
                    String cellValue = "";
                    if (value instanceof Date) {
                        Date date = (Date) value;
                        cellValue = sd.format(date);
                    } else {
                        cellValue = null != value ? value.toString() : "";
                    }
                    Cell cell = row.createCell(i);
                    if (CustomerTypeEnum.BUYER.getCode().equals(creditExcelVO.getCustomerType())) {
                        XSSFFont font = workbook.createFont();
                        font.setColor(HSSFColor.RED.index);
                        font.setFontHeightInPoints((short) 12);
                        font.setFontName("宋体");
                        style.setFont(font);
                        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
                    } else {
                        XSSFFont font = workbook.createFont();
                        font.setColor(HSSFColor.BLACK.index);
                        font.setFontHeightInPoints((short) 12);
                        font.setFontName("宋体");
                        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
                        style.setFont(font);
                    }
                    cell.setCellStyle(style);
                    cell.setCellValue(cellValue);

                } catch (Exception e) {
                    log.error("ExcelUtil creatAuditSheet方法出错",e);
                }
            }
        }

        return sheet;
    }
}